package com.cskaoyan.controller.admin;

import com.cskaoyan.model.admin.bo.*;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.BaseRespVo;

import com.cskaoyan.model.admin.vo.GoodsDetailVo;

import com.cskaoyan.model.admin.vo.CatAndBrandVo2;
import com.cskaoyan.service.admin.GoodsAdminGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RequestMapping("admin/goods")
@RestController
public class GoodsAdminGoodsController {
    @Autowired
    GoodsAdminGoodsService goodsAdminGoodsService;

    @RequestMapping("list")
    public BaseRespVo list(ListBo listBo, String goodsSn, String name, String goodsIdStr) {

        Integer goodsId = null;
        try {
            if(goodsId != null){
                goodsId = Integer.parseInt(goodsIdStr);
            }

        } catch (NumberFormatException e) {
            e.printStackTrace();
            return BaseRespVo.fail(404,"参数异常");
        }
        BaseData goodsBaseData = goodsAdminGoodsService.list(listBo, goodsSn, name, goodsId);
        if(goodsBaseData.getTotal() == 0)return BaseRespVo.fail(404,"输入参数类型有误");
        // http://182.92.235.201:8083/admin/goods/list?
        // page=1&limit=20&goodsSn=11&name=%E9%A9%AC%E4%B8%81&sort=add_time&order=desc&goodsId=11
        return BaseRespVo.ok(goodsBaseData);
    }

    //    @PostMapping("create")
//    public BaseRespVo create(@RequestBody MarketGoods marketGoods,  List< MarketGoodsSpecification> goodsSpecificationList
//                             , List<MarketGoodsProduct> goodsProductList, List<MarketGoodsAttribute> goodsAttributeList){
//        Integer row = goodsAdminGoodsService.create(marketGoods);
//        if(row != 1) return BaseRespVo.fail(404,"添加失败");
//        return BaseRespVo.ok(200);
//    }
    @PostMapping("create")
    public BaseRespVo create(@RequestBody GoodsCreateBo goodsCreateBo) {
        Integer row = goodsAdminGoodsService.create(goodsCreateBo);
        if(row == 1){
            return BaseRespVo.ok(200);
        }
        return BaseRespVo.fail(404,"添加失败");
    }
//    @GetMapping("catAndBrand")
//    public BaseRespVo catAndBrand(){
//        CatAndBrandVo catAndBrandVo = goodsAdminGoodsService.catAndBrand();
//        return BaseRespVo.ok(catAndBrandVo);
//    }
    @GetMapping("catAndBrand")
    public BaseRespVo catAndBrand(){
        CatAndBrandVo2 catAndBrandVo2 = goodsAdminGoodsService.catAndBrand2();
        return BaseRespVo.ok(catAndBrandVo2);
    }
    @PostMapping("update")
    public BaseRespVo update(@RequestBody GoodsUpdateBO goodsUpdateBO){

        goodsAdminGoodsService.update(goodsUpdateBO);
        return BaseRespVo.ok(null);
    }
    @GetMapping("detail")
    public BaseRespVo detail(Integer id){
        GoodsDetailVo goodsDetailVo=goodsAdminGoodsService.detail(id);
        return BaseRespVo.ok(goodsDetailVo);
    }
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        Object id = map.get("id");
        Integer goodsId = null;
        try{
            goodsId = (Integer) id;
        }catch (Exception e){
            e.printStackTrace();
            return BaseRespVo.fail(404,"参数出错");
        }
        Integer code = goodsAdminGoodsService.delete(goodsId);
        if(code == 1)return BaseRespVo.ok(null);
       return BaseRespVo.fail(404,null);
    }
}
